﻿using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Dao
{
    public class ErrorDao
    {
        public int insertError(DeviceFault deviceFault)
        {
            using (var db = SqlSugarConfig.Db)
            {
                Tools.ProduceLogText($"采集到一条报警信息，报警信息为{deviceFault.FaultMessage}");
                return db.Insertable(deviceFault).ExecuteReturnIdentity();

            }
        }

        public void updateError(DateTime endTime, int value)
        {
            using (var db = SqlSugarConfig.Db)
            {
                var fault = db.Queryable<DeviceFault>()
                         .Where(x => x.Id == value)
                         .First();

                string timeSpan = Tools.CalculateTimeDifferenceInMinutes(fault.StopStartTime, endTime);
               
                DeviceFault deviceFault = new DeviceFault
                {
                    Id = value,
                    StopEndTime = endTime,
                    FaultTime = timeSpan
                };

                db.Updateable(deviceFault)
                .UpdateColumns(x => new { x.StopEndTime, x.FaultTime }) // 指定只更新EndTime
                .Where(x => x.Id == value).ExecuteCommand();
                Tools.ProduceLogText($"解除了一条报警信息，持续时长为{deviceFault.FaultTime}");
            }
        }
    }
}
